package com.ceba.test.modules.invalid.dao;


import com.ceba.base.web.dao.IBaseDao;
import com.ceba.cebautils.StringUtil;
import com.ceba.forveri.bean.SigntureVeriResult;
import com.ceba.test.modules.filemanage.entity.GFile;
import com.ceba.test.modules.invalid.entity.ReportInvalid;

import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;

public class ReportInvalidDao extends IBaseDao {
	
	private String tableName = "cb_report_invalid";
	
	/*
	 * 判断主键是否存在
	 */
	public boolean checkReportInvalidIdExsit(String id){
		return SqlSessionManager.getCount(new Sql("select count(ID) from "+tableName+ " where ID='"+id+"'"))>0?true:false;
	}
	
	/*
	 * 删除
	 */
	public boolean deleteReportByReportId(String reportId) {
		Sql sql = new Sql("delete from "+tableName + " where ID = '"+reportId+"'" );
		return SqlSessionManager.execute(sql);
	}

	/**
	 * 通过id查找
	 * @param reportId
	 */
	public ReportInvalid getInvalidReportByReportId(String reportId) {
		
		ReportInvalid reportInvalid = new ReportInvalid ();
		Sql sql=new Sql(" select * from "+tableName+"  WHERE ID = '"+reportId+"'");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		if(dt !=null && dt.getRowCount() >0){
			DataRow dr = dt.getRow(0);
			reportInvalid.setId(dr.getString("ID"));
			reportInvalid.setReportId(dr.getString("REPORTID"));
			reportInvalid.setAlg(dr.getString("ALG"));
			reportInvalid.setCerNum(dr.getInt("CER_NUM"));
			reportInvalid.setCode(dr.getInt("CODE"));
			reportInvalid.setFileName(dr.getString("FILE_NAME"));
			reportInvalid.setIssuer(dr.getString("ISSUER"));
			reportInvalid.setIssuerO(dr.getString("ISSUER_O"));
			reportInvalid.setObjectId(dr.getString("OBJECTID"));
			reportInvalid.setSignTime(dr.getString("SIGN_TIME"));
			reportInvalid.setSubject(dr.getString("SUBJECT"));
			reportInvalid.setSubjectO(dr.getString("SUBJECT_O"));
			reportInvalid.setTrustFlag(dr.getInt("TRUST_FLAG"));
			reportInvalid.setTrustState(dr.getString("TRUST_STATE"));
			reportInvalid.setValidEndTime(dr.getString("VALID_ENDTIME"));
			reportInvalid.setValidStartTime(dr.getString("VALID_STARTTIME"));
			reportInvalid.setVeriResult(dr.getString("VERFI_RESULT"));
			reportInvalid.setInvalidFileId(dr.getString("INVALID_FILE_ID"));
			reportInvalid.setAnnex1FileId(dr.getString("ANNEX1_FILE_ID"));
			reportInvalid.setAnnex2FileId(dr.getString("ANNEX2_FILE_ID"));
			reportInvalid.setStatus(dr.getString("STATUS"));
			reportInvalid.setFileHash(dr.getString("FILE_HASH"));
			reportInvalid.setReason(dr.getString("REASON"));
			reportInvalid.setUploadUserIp(dr.getString("UPLOAD_USER_IP"));
			reportInvalid.setCreatTime(dr.getString("CREATE_TIME"));
			reportInvalid.setCreatUser(dr.getString("CREATE_USER"));
			return reportInvalid;
		}
		return null;
	}
	
	
	/*
	 * 更新作废文档附件
	 * annix == 1 附件1
	 * annix == 2 附件2
	 */
	public boolean updateReportInValidByAnnix(GFile gFile, String reportId, int annix,boolean suc) throws Exception{
		IStore store = StoreManager.create(tableName);
		IObject object = new XObject(tableName);
		object.put("ID", reportId);
		if(suc){
			if(annix == 1){
				object.put("ANNEX1_FILE_ID", gFile.getId());
			}else if(annix == 2){
				object.put("ANNEX2_FILE_ID", gFile.getId());
			}
			if(store.update(object)>0){
				return true;
			}
		}
		return false;
	}
	
	/*
	 * 更新作废文档信息
	 */
	public boolean updateReportInValid(ReportInvalid reportInvalid, SigntureVeriResult svr) throws Exception{
		reportInvalid = packReportInValidFromSig(reportInvalid,svr);
		IStore store = StoreManager.create(tableName);
		IObject object = new XObject(tableName);
		object.put("ID", reportInvalid.getId());
		object.put("ALG", reportInvalid.getAlg());
		object.put("CER_NUM", reportInvalid.getCerNum());
		object.put("CODE", reportInvalid.getCode());
		object.put("ISSUER", reportInvalid.getIssuer());
		object.put("ISSUER_O", reportInvalid.getIssuerO());
		object.put("OBJECTID", reportInvalid.getObjectId());
		if(!StringUtil.isEmptyString(reportInvalid.getSignTime())){
			object.put("SIGN_TIME", reportInvalid.getSignTime());
		}
		object.put("SUBJECT", reportInvalid.getSubject());
		object.put("SUBJECT_O", reportInvalid.getSubjectO());
		object.put("TRUST_FLAG", reportInvalid.getTrustFlag());
		object.put("TRUST_STATE", reportInvalid.getTrustState());
		object.put("VALID_ENDTIME", reportInvalid.getValidEndTime());
		object.put("VALID_STARTTIME", reportInvalid.getValidStartTime());
		object.put("VERFI_RESULT", reportInvalid.getVeriResult());
		object.put("FILE_HASH", reportInvalid.getFileHash());
		
		if(store.update(object)>0){
			return true;
		}
		return false;
	}
	
	
	
	public ReportInvalid packReportInValidFromSig(ReportInvalid reportInvalid, SigntureVeriResult svr){
		reportInvalid.setAlg(svr.getAlg());
		reportInvalid.setCerNum(svr.getCerNum());
		reportInvalid.setCode(svr.getCode());
		reportInvalid.setIssuer(svr.getIssuer());
		reportInvalid.setIssuerO(svr.getIssuer_o());
		reportInvalid.setObjectId(svr.getObjectId());
		reportInvalid.setSignTime(svr.getSignTime());
		reportInvalid.setSubject(svr.getSubject());
		reportInvalid.setSubjectO(svr.getSubject_o());
		reportInvalid.setTrustFlag(svr.getTruestFlay());
		reportInvalid.setTrustState(svr.getTrustState());
		reportInvalid.setValidEndTime(svr.getValideEndTimeAfterParse());
		reportInvalid.setValidStartTime(svr.getValidStartTimeAfterParse());
		reportInvalid.setVeriResult(svr.getVerfiResult());
		reportInvalid.setFileHash(svr.getFileHash());
		return reportInvalid;
		
	}
	
	
	/**
	 * 添加一条记录
	 * @return
	 */
	public Boolean add(ReportInvalid rpi) throws Exception{
		IStore store = StoreManager.create(tableName);
		IObject object = new XObject(tableName);
		object.put("ID",rpi.getId());
		object.put("REPORTID", rpi.getReportId());
		object.put("ISSUER",rpi.getIssuer());
		object.put("ISSUER_O",rpi.getIssuerO());
		object.put("SUBJECT",rpi.getSubject());
		object.put("SUBJECT_O",rpi.getSubjectO());
		object.put("SIGN_TIME",rpi.getSignTime());
		object.put("VALID_STARTTIME",rpi.getValidStartTime());
		object.put("VALID_ENDTIME",rpi.getValidEndTime());
		object.put("VERFI_RESULT",rpi.getVeriResult());
		object.put("OBJECTID",rpi.getObjectId());
		object.put("ALG",rpi.getAlg());
		object.put("CODE",rpi.getCode());
		object.put("FILE_NAME",rpi.getFileName());
		object.put("TRUST_STATE",rpi.getTrustState());
		object.put("CER_NUM",rpi.getCerNum());
		object.put("TRUST_FLAG",rpi.getTrustFlag());
		object.put("INVALID_FILE_ID",rpi.getInvalidFileId());
		object.put("ANNEX1_FILE_ID",rpi.getAnnex1FileId());
		object.put("ANNEX2_FILE_ID",rpi.getAnnex2FileId());
		object.put("REASON",rpi.getReason());
		object.put("STATUS",rpi.getStatus());
		object.put("FILE_HASH", rpi.getFileHash());
		object.put("UPLOAD_USER_IP",rpi.getUploadUserIp());
		object.setCreateTime();
		object.setCreateUser();
		
		if(store.insert(object)>0){
			return true;	
		}
		return false;
	}
	
	/**
	 * 更新某个属性的值
	 * @param filed
	 * @param value
	 * @return
	 */
	public Boolean updateFiledValue(String id,String filed,String value) throws Exception{
		return  updateFiledValue(tableName, id, filed, value);
	}
}
